﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using System.IO;

namespace ZLDC.Common.BaseUser
{
    public partial class PageBase : System.Web.UI.Page
    {
        protected Param info;
        protected string pdmNo = "";
        protected override void OnInit(EventArgs e)
        {
            info = GetProfile();
            if (string.IsNullOrEmpty(info.m_sUserID))
            {
                Response.Write("未获得权限");
                Response.End();
            }
            base.OnInit(e);

        }

        internal static Param GetProfile()
        {

            Param profile = new Param();
            string id;


            if (HttpContext.Current.Session["info"] == null)
            {
                id = HttpContext.Current.Session["key"].ToString();
                UserInfo.GetInfo(id, ref profile);
                HttpContext.Current.Session.Add("info", profile);
            }
            else
            {
                profile = (Param)HttpContext.Current.Session["info"];
            }


            return profile;

        }

        protected string GetMessage(string strMsgs)
        {
            System.Text.StringBuilder strMsg = new StringBuilder();
            strMsg.Append("  <table cellspacing=\"1\" cellpadding=\"0\" width=\"500\" align=\"center\" border=\"0\">");
            strMsg.Append("                     <tr>");
            strMsg.Append("                         <td align=\"center\">");
            strMsg.Append("                             <table style=\"border-style: dotted; border-width: 1px; width: 260px; height: 180px;\"");
            strMsg.Append("                                 border=\"0\" align=\"center\" cellspacing=\"1\" cellpadding=\"5\">");
            strMsg.Append("                                 <tr>");
            strMsg.Append("                                     <td align=\"center\">");
            strMsg.Append("                                         <img style=\"margin: 5px; float: center;\" src=\"../../images/warning.gif\" />");
            strMsg.Append("                                     </td>");
            strMsg.Append("                                     <td align=\"left\">");
            strMsg.Append("                                         <span style=\"word-wrap: bread-word; word-break: break-all; font-size: 12px;\"><strong>");
            strMsg.Append("                                          " + (strMsgs == "" ? "您没有权限运行当前程序,请您和管理员联系!" : strMsgs));
            strMsg.Append("                                         </strong></span>");
            strMsg.Append("                                     </td>");
            strMsg.Append("                                 </tr>");
            strMsg.Append("                             </table>");
            strMsg.Append("                         </td>");
            strMsg.Append("                     </tr>");
            strMsg.Append("                 </table>");
            return strMsg.ToString();
        }

        #region 根据员工得部门名称
        protected string GetDeptName(string UserID)
        {
            string strSql = "select * from t_zlhq_t_s_alldept where id in (select t_id from t_s_allusr where id=" + UserID + ")";
            DataBaseAll db = new DataBaseAll("ZLDCHR");
            DataTable dt = db.SelectDataBaseAll(strSql).Tables[0];
            if (dt.Rows.Count > 0)
                return dt.Rows[0]["name"].ToString();
            else
                return "";
        }

        protected string GetDeptNameByDeptID(string DeptID)
        {
            string strSql = "select * from t_zlhq_t_s_alldept where id=" + DeptID;
            DataBaseAll db = new DataBaseAll("ZLDCHR");
            DataTable dt = db.SelectDataBaseAll(strSql).Tables[0];
            if (dt.Rows.Count > 0)
                return dt.Rows[0]["name"].ToString();
            else
                return "";
        }

        protected DataTable GetUserInfo(string UserID)
        {
            string strSql = "select * from t_s_allusr where id=" + UserID;
            DataBaseAll db = new DataBaseAll("ZLDCHR");
            DataTable dt = db.SelectDataBaseAll(strSql).Tables[0];

            return dt;
        }

        protected string GetDataDept()
        {
            string strSql = "select * from [kfb].[zldc_t_s_dept] where id=" + this.info.m_sDeptID;
            DataTable dt = DbHelperSQL.Query(strSql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                return " and ( Code like '" + dt.Rows[0]["Code"].ToString() + "%' )";
            }
            else
            {
                return "";
            }
        }

        protected string GetDataDept(string TableName)
        {
            string strSql = "select * from [kfb].[zldc_t_s_dept] where id=" + this.info.m_sDeptID;
            DataTable dt = DbHelperSQL.Query(strSql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                return " and ( " + TableName + ".Code like '" + dt.Rows[0]["Code"].ToString() + "%' )";
            }
            else
            {
                return "";
            }
        }

        protected string GetDataCrossDept()
        {
            string strSql = @" select A.code from zldc_t_s_dept  A
                             inner join zldc.t_s_Employee_Dept B on B.DeptID=A.OrgID
                             where B.EmpID=" + this.info.m_sUserID +
                           " union all" +
                           " select code from [kfb].[zldc_t_s_dept] where id=" + this.info.m_sDeptID;
            DataTable dt = DbHelperSQL.Query(strSql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                string str = " and (";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i == dt.Rows.Count - 1)
                        str += " Code like '" + dt.Rows[i]["Code"].ToString() + "%'";
                    else
                        str += " Code like '" + dt.Rows[i]["Code"].ToString() + "%' or ";
                }
                return str + " )";
            }
            else
            {
                return "";
            }
        }

        protected string GetDataCrossDept(string tableName)
        {
            string strSql = @" select A.code from zldc_t_s_dept  A
                             inner join zldc.t_s_Employee_Dept B on B.DeptID=A.OrgID
                             where B.EmpID=" + this.info.m_sUserID +
                           " union all" +
                           " select code from [kfb].[zldc_t_s_dept] where id=" + this.info.m_sDeptID;
            DataTable dt = DbHelperSQL.Query(strSql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                string str = " and (";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i == dt.Rows.Count - 1)
                        str += "" + tableName + ".Code like '" + dt.Rows[i]["Code"].ToString() + "%'";
                    else
                        str += "" + tableName + ".Code like '" + dt.Rows[i]["Code"].ToString() + "%' or ";
                }
                return str + " )";
            }
            else
            {
                return "";
            }
        }
        #endregion

        protected internal string CopyRight = "CreateBy ZLHQ.Studio";
        public Single sngPageRunTime = (Single)0.00;
        public DateTime dtStartTime;

        #region "计算页面执行时间"
        protected void Page_Init(Object sender, EventArgs e)
        {
            dtStartTime = DateTime.Now;
        }



        #endregion

        #region "页面错误处理"
        protected void Page_Error(object sender, EventArgs e)
        {
            Server.Transfer("~/ErrorPage.aspx");
        }
        #endregion
    }

    public partial class PageBase : System.Web.UI.Page
    {
        public virtual void ShowAlertScriptToClient(string msg, string urlOrscript)
        {
            Response.Write(string.Format("<script type=\"text/javascript\"> alert('{0}');{1};</script>", msg, urlOrscript));
            Response.End();
        }

        public virtual void ShowAlertScriptToClient(string msg)
        {
            Response.Write(string.Format("<script type=\"text/javascript\"> alert('{0}');location.href=location.href;</script>", msg));
            Response.End();
        }

        public virtual void ShowAlertScriptToClientRefTopPage(string msg)
        {
            Response.Write(string.Format("<script type=\"text/javascript\"> alert('{0}');top.location.href=top.location.href;</script>", msg));
            Response.End();
        }
    }
}
